//
//  YCIMViewController.m
//  DemoYTX
//
//  Created by 唐可雄 on 16/7/6.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "YCIMViewController.h"

@interface YCIMViewController () {
    /// 0/1/2，消息/联系人/群组
    NSInteger _curIndex;
    /// 当前的内容视图
    UIViewController *_curContentVC;
}

/// 消息
@property (strong, nonatomic) UIViewController *vcMessage;
/// 联系人
@property (strong, nonatomic) UIViewController *vcPerson;
/// 群组
@property (strong, nonatomic) UIViewController *vcGroup;

@end

@implementation YCIMViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configMessagBadgeView];
    
    [self messageBtnDidClick:self.messageBtn];
}

#pragma mark - Setter and Getter

- (UIViewController *)vcMessage {
    if (_vcMessage == nil) {
        _vcMessage = [self.storyboard instantiateViewControllerWithIdentifier:@"vcMessage"];
    }
    
    return _vcMessage;
}

- (UIViewController *)vcPerson {
    if (_vcPerson == nil) {
        _vcPerson = [self.storyboard instantiateViewControllerWithIdentifier:@"vcPerson"];
    }
    
    return _vcPerson;
}

- (UIViewController *)vcGroup {
    if (_vcGroup == nil) {
        _vcGroup = [self.storyboard instantiateViewControllerWithIdentifier:@"vcGroup"];
    }
    
    return _vcGroup;
}

#pragma mark - View Config

/// 设置消息数量标签
- (void)configMessagBadgeView {
    self.messageBadgeView.clipsToBounds = YES;
    self.messageBadgeView.layer.cornerRadius = 8.f;
    
    self.messageBadgeLabel.text = @"12";
}

/// 改变内容视图
- (void)changeContentView {
    if (_curIndex == 0) {
        [self showContentVC:self.vcMessage];
    } else if (_curIndex == 1) {
        [self showContentVC:self.vcPerson];
    } else {
        [self showContentVC:self.vcGroup];
    }
}

/// 设置标题栏
- (void)changeTitleView {
    if (_curIndex == 0) {
        [self.messageBtn setTitleColor:[UIColor colorWithRed:0. green:122./255. blue:1.0 alpha:1.0] forState:UIControlStateNormal];
        [self.linkmanBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.groupBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        self.constraintLineViewLeading.constant = 0;
    } else if (_curIndex == 1) {
        [self.messageBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.linkmanBtn setTitleColor:[UIColor colorWithRed:0. green:122./255. blue:1.0 alpha:1.0] forState:UIControlStateNormal];
        [self.groupBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        self.constraintLineViewLeading.constant = 60;
    } else {
        [self.messageBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.linkmanBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.groupBtn setTitleColor:[UIColor colorWithRed:0. green:122./255. blue:1.0 alpha:1.0] forState:UIControlStateNormal];
        
        self.constraintLineViewLeading.constant = 120;
    }
    
    [self.view layoutIfNeeded];
}

#pragma mark - Model Methods

/// 删除内容视图
- (void)removeContentVC {
    if (_curContentVC) {
        [_curContentVC removeFromParentViewController];
        [_curContentVC.view removeFromSuperview];
    }
}

/// 安装内容视图
- (void)setupContentVC {
    [self.contentView addSubview:_curContentVC.view];
    [self addChildViewController:_curContentVC];
    
    _curContentVC.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_curContentVC.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_curContentVC.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_curContentVC.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_curContentVC.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
}

/// 显示内容视图控制器
- (void)showContentVC:(UIViewController *)vc {
    [self removeContentVC];
    
    _curContentVC = vc;
    
    [self setupContentVC];
}

#pragma mark - IBAction

/// 点击消息按钮
- (IBAction)messageBtnDidClick:(id)sender {
    _curIndex = 0;
    
    [self changeTitleView];
}

/// 点击联系人按钮
- (IBAction)linkmanBtnDidClick:(id)sender {
    _curIndex = 1;
    
    [self changeTitleView];
}

/// 点击群组按钮
- (IBAction)groupBtnDidClick:(id)sender {
    _curIndex = 2;
    
    [self changeTitleView];
}

@end
